﻿@model Xms.Web.Customize.Models.LocalizationLabelsModel

<div class="panel panel-default" id="gridview">
    <div class="panel-heading">
        <div class="panel-title">
            <strong>@app.PrivilegeTree?.LastOrDefault().DisplayName</strong>
            <div class="pull-right">
                <a class="btn btn-info btn-xs" data-toggle="collapse"
                   href="#collapseOne">
                    <span class="caret"></span> @app.T["search"]
                </a>
                <a class="btn btn-info btn-xs" href="javascript:;" onclick="exportLabels()"><span class="glyphicon glyphicon-export"></span> @app.T["export"]</a>
                <a class="btn btn-info btn-xs" href="#" data-toggle="modal" data-target="#importModal"><span class="glyphicon glyphicon-import"></span> @app.T["import"]</a>
            </div>
        </div>
    </div>
    <div id="collapseOne" class="panel-collapse collapse in">
        <!--in-->
        @{
            var edens = "";
        }
        <div class="panel-body">
            <form action="/@app.OrganizationUniqueName/customize/@app.ControllerName/@app.ActionName" method="get" id="searchForm" class="form-horizontal" role="form">
                @Html.ValidationSummary()
                @Html.HiddenFor(x => x.SolutionId)
                <div class="form-group col-sm-6">
                    <label class="col-sm-3 control-label" for="typecode">@app.T["typename"]</label>
                    <div class="col-sm-9">
                        <select class="form-control" name="typecode" id="typecode">
                            <option value="">请选择</option>
                            @foreach (var item in Xms.Solution.Abstractions.SolutionComponentCollection.Descriptors)
                            {
                                edens += item.Value.Module.Identity + ":" + @app.T[item.Key].IfEmpty(item.Key) + ",";
                                <option value="@item.Value.Module.Identity">@app.T[item.Key].IfEmpty(item.Key)</option>
                            }
                        </select>
                    </div>
                </div>
                <div class="form-group col-sm-6">
                    <button type="submit" class="btn btn-primary btn-block"><span class="glyphicon glyphicon-search"></span> @app.T["search"]</button>
                </div>
            </form>
            <input id="edens" value="@edens" type="hidden" />
        </div>
    </div>
    <div class="table-datagrid-view">
        <div class="datagrid-view"></div>
        <table class="table table-hover table-striped table-condensed" id="datatable" data-refresh="rebind()" data-ajax="true" data-ajaxcontainer="gridview" data-ajaxcallback="ajaxgrid_reset()" data-sortby="@Model.SortBy.ToLower()" data-sortdirection="@Model.SortDirection" data-pageurl="@app.Url">
            <thead>
                <tr>
                    <th width="2%"><input type="checkbox" name="checkall" /></th>
                    <th data-name="labeltypecode">
                        @app.T["typename"]
                    </th>
                    <th data-name="objectcolumnname">
                        @app.T["name"]
                    </th>
                    <th width="15%" data-name="label">
                        @app.T["label"]
                    </th>
                    <th width="15%" data-name="languageid">
                        @app.T["language"]
                    </th>
                    <th width="15%">@app.T["operation"]</th>
                </tr>
            </thead>
            <tbody>
                @foreach (var item in Model.Items)
                {
                    var descriptor = Xms.Solution.Abstractions.SolutionComponentCollection.Descriptors.Values.FirstOrDefault(x => x.Module.Identity == item.LabelTypeCode);
                    if (descriptor == null)
                    {
                        continue;
                    }
                    <tr data-dbclick="location.href='@("/"+app.OrganizationUniqueName)/customize/LocalizedLabel/UpdateLocalizationLabel?objectid=@item.ObjectId&columnname=@item.ObjectColumnName'">
                        <td><input type="checkbox" value="@item.LocalizedLabelId" name="recordid" /></td>
                        <td>
                            @(descriptor.Module.Name)
                        </td>
                        <td data-name="objectcolumnname">@item.ObjectColumnName</td>
                        <td>@item.Label</td>
                        <td>@item.LanguageId</td>
                        <td>
                            <a class="btn btn-default btn-xs" href="@("/"+app.OrganizationUniqueName)/customize/LocalizedLabel/UpdateLocalizationLabel?objectid=@item.ObjectId&columnname=@item.ObjectColumnName"><span class="glyphicon glyphicon-edit"></span> @app.T["edit"]</a>
                        </td>
                    </tr>
                }
            </tbody>
        </table>
    </div>
    <div class="panel-footer">
        <div class="row">
            <div class="col-sm-4">
                @(app.T["pagination_label"].Replace("{page}", Model.Page.ToString()).Replace("{totalpages}", Model.TotalPages.ToString()).Replace("{totalitems}", Model.TotalItems.ToString()))
            </div>
            <div id="page-selection" class="col-sm-8" data-total="@Model.TotalPages" data-page="@Model.Page"></div>
        </div>
    </div>
</div>
<!-- （Modal） -->
<div class="modal fade" id="importModal" tabindex="-1" role="dialog"
     aria-labelledby="importModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal"
                        aria-hidden="true">
                    @app.T["times_sign"]
                </button>
                <h4 class="modal-title" id="navGroupModalLabel">
                    @app.T["import"]
                </h4>
            </div>
            <div class="modal-body">
                <form id="importForm" action="@("/"+app.OrganizationUniqueName)/customize/LocalizedLabel/ImportLocalizationLabels" method="post" enctype="multipart/form-data">
                    @Html.HiddenFor(x => x.SolutionId)
                    <div class="form-group col-sm-12">
                        <span class="glyphicon glyphicon-warning-sign"></span>
                        <span class="text-muted">
                            @app.T["localization_import_tip"]
                        </span>
                    </div>
                    <div class="form-group">
                        <label for="col-sm-2 control-label">@app.T["file"]</label>
                        <div class="col-sm-10">
                            <input id="uploadfile" name="uploadfile" type="file" class="form-control input-sm" />
                        </div>
                    </div>
                </form>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default"
                        data-dismiss="modal">
                    <span class="glyphicon glyphicon-remove"></span> @app.T["cancel"]
                </button>
                <button type="button" class="btn btn-primary" onclick="importLabels()">
                    <span class="glyphicon glyphicon-ok"></span> @app.T["confirm"]
                </button>
            </div>
        </div><!-- /.modal-content -->
    </div><!-- /.modal-dialog -->
</div><!-- /.modal -->
@section Header {
    <link href="~/content/customize/css/common.css?v=@app.PlatformSettings.VersionNumber" rel="stylesheet">
    <link href="~/content/js/jquery-ui-1.10.3/themes/base/jquery.ui.all.css?v=@app.PlatformSettings.VersionNumber" rel="stylesheet">
    <link href="~/content/js/grid/pqgrid.dev.css?v=@app.PlatformSettings.VersionNumber" rel="stylesheet">
    <link id="themeLink" href="~/content/css/theme/@(app.Theme).css" rel="stylesheet" />
}
@section Scripts {
    <script src="/content/js/jquery-ui-1.10.3/ui/jquery.ui.button.js?v=@app.PlatformSettings.VersionNumber"></script>
    <script src="/content/js/jquery-ui-1.10.3/ui/jquery.ui.mouse.js?v=@app.PlatformSettings.VersionNumber"></script>
    <script src="/content/js/jquery-ui-1.10.3/ui/jquery.ui.autocomplete.js?v=@app.PlatformSettings.VersionNumber"></script>
    <script src="/content/js/jquery-ui-1.10.3/ui/jquery.ui.draggable.js?v=@app.PlatformSettings.VersionNumber"></script>
    <script src="/content/js/jquery-ui-1.10.3/ui/jquery.ui.resizable.js?v=@app.PlatformSettings.VersionNumber"></script>
    <script src="/content/js/jquery-ui-1.10.3/ui/jquery.ui.tooltip.js?v=@app.PlatformSettings.VersionNumber"></script>
    <script src="~/content/js/fetch.js?v=@app.PlatformSettings.VersionNumber"></script>
    <script src="~/content/js/common/filters.js?v=@app.PlatformSettings.VersionNumber"></script>
    <script src="/content/js/grid/pqgrid.dev.js?v=@app.PlatformSettings.VersionNumber"></script>
    <script src="/content/js/grid/localize/pq-localize-zh.js?v=@app.PlatformSettings.VersionNumber"></script>
    <script src="/content/js/cdatagrid.js?v=@app.PlatformSettings.VersionNumber"></script>
    <script src="/content/js/jquery.bootpag.min.js?v=@app.PlatformSettings.VersionNumber"></script>
    <script src="/content/js/jquery.form.js?v=@app.PlatformSettings.VersionNumber"></script>
    <script src="~/content/js/jquery-validate/jquery.validate.min.js?v=@app.PlatformSettings.VersionNumber"></script>
    <script>
    var pageUrl = '';
     var solutionid = '@Model.SolutionId';
        var websourcetypes = [{ label: '脚本', value: 'Script' }, { label: '样式', value: 'Css' }, { label: '网页', value: 'Html' }, { label: '图片', value: 'Picture' }]
        var edens = $('#edens').val();
        var arr_edens = edens.split(',');
        var labeltypes = [];
        $.each(arr_edens, function (i,n) {
            if (n != '' && n.indexOf(':')) {
                var temp = n.split(':');
                labeltypes[temp[0]] = {
                    label: temp[1],
                    value: temp[0]
                }
            }
        });
        console.log(labeltypes)
        $(function () {
            //pageUrl = $("#datatable").attr('data-pageurl');
            //$('#searchForm').ajaxSearch('#gridview', ajaxgrid_reset);
            //$("#datatable").ajaxTable();
            //ajaxgrid_reset();
            var theaders = {
                'typename':'@app.T["typename"]',
                'name': '@app.T["name"]',
                'label': '@app.T["label"]',
                'language': '@app.T["language"]',
                'operation':'@app.T["operation"]'
            }
            //列数据配置数据
            var columnConfigs = [
            //从新配置复选框列的渲染方式，
                {
                    title: "", dataIndx: "recordid", maxWidth: 48, minWidth: 48, align: "center", resizable: false,
                    type: 'checkBoxSelection', cls: 'ui-state-default', sortable: false, editable: false,
                    render: function (ui) {
                      //  console.log(ui)
                        return '<input type="checkbox" value="' + ui.rowData.webresourceid + '" name="recordid" class="">'
                    },
                    cb: { all: true, header: true }
                },
                {
                    title: "", dataIndx: "name", maxWidth: 30, minWidth: 30, align: "center", resizable: false,
                     cls: 'ui-state-default', sortable: false, editable: false,
                    hidden:true,
                    render: function (ui) {
                        //  console.log(ui)
                        return '<input type="hidden" value="' + ui.rowData.webresourceid + '" name="componenttypename" class="">'
                    },
                    cb: { all: true, header: true }
                },
                 { "dataIndx": "labeltypecode", "title": theaders.typename, editable: false, "dataType": "string", "width": 150, "isprimaryfield": false, "attributetypename": "string" , render: function (ui) {
                        var datas = ui.rowData;
                        var dataIndx = ui.dataIndx;
                        var column = ui.column;
                        var recordid = datas[dataIndx];
                     var html = ''
                     if (labeltypes[recordid]) {
                         html += labeltypes[recordid].label;
                         return html
                     }
                    }},
                { "dataIndx": "objectcolumnname", "title": theaders.name, editable: false, "dataType": "string", "width": 150, "isprimaryfield": false, "attributetypename": "string" },
                { "dataIndx": "label", "title": theaders.label, "dataType": "string", editable: false, "width": 150, "isprimaryfield": false, "attributetypename": "string" },

                 { "dataIndx": "language", "title": theaders.language, editable: false, "dataType": "string", "width": 150, "isprimaryfield": false, "attributetypename": "string" },
                {
                    title: "操作", editable: false, minWidth: 120,width:120, notHeaderFilter: true, editable: false, sortable: false, render: function (ui) {
                        var datas = ui.rowData;
                        var dataIndx = ui.dataIndx;
                        var column = ui.column;
                        var recordid = datas[dataIndx];
                        var html = ''
                        html=  ' <a class="btn btn-default btn-xs" href="@("/"+app.OrganizationUniqueName)/customize/LocalizedLabel/UpdateLocalizationLabel?objectid='+datas.objectid+'&columnname='+datas.objectcolumnname+'" ><span class="glyphicon glyphicon-edit"></span> 编辑</a>'
                        return html
                    }
                }
            ];
           var url = ORG_SERVERURL + '/customize/@app.ControllerName/@app.ActionName';
            var $form = $('#searchForm');
            var roles_filters = new XmsFilter();
            var datagridconfig = {
                scrollModel: { autoFit: true },
                baseUrl: url,
                columnConfigs: columnConfigs,//字段配置信息
                context: $('#gridview'),//底部操作按钮方法触发
                filters: roles_filters,//post提交时过滤条件
                searchForm: $form//GET提交时查询的数据
            };
        //    $('.datagrid-view').xmsDataTable(datagridconfig);
        pageUrl = $("#datatable").attr('data-pageurl');
        $('#searchForm').ajaxSearch('#gridview', ajaxgrid_reset);
        $("#datatable").ajaxTable();
            ajaxgrid_reset();
            Xms.Web.Event.localStorageEvent.on('list_localizelabel_rebind', function () {
                setTimeout(function () {
                    location.reload();
                },100)
            });
    });
    function ajaxgrid_reset() {
        pag_init();
        Xms.Web.DataTable($("#datatable"));
    }
    function pag_init() {
        $('#page-selection').bootpag({
            total: $('#page-selection').attr('data-total')
            , maxVisible: 10
            , page: $('#page-selection').attr('data-page')
            , leaps: false
            , prev: '&lsaquo;'
            , next: '&rsaquo;'
            , firstLastUse: true
            , first: '&laquo;'
            , last: '&raquo;'
        }).on("page", function (event, /* page number here */ num) {
            event.preventDefault();
            pageUrl = $("#datatable").attr('data-pageurl');
            var url = $.setUrlParam(pageUrl, 'page', num);
            $("#gridview").ajaxLoad(url, "#gridview", function (response) {
                ajaxgrid_reset();
            });
            return false;
        });
    }
    function rebind() {
        $('#searchForm').submit();
    }
    function exportLabels() {
        Xms.Web.Confirm('@app.T["export"]', '@app.T["localization_export_tip"]', function () {
            location.href = ORG_SERVERURL + '/customize/LocalizedLabel/exportlocalizationlabels?solutionid=@Model.SolutionId';
        });
        return false;
    }
    function importLabels() {
        $('#importForm').ajaxSubmit(function (response) {
            console.log(response);
            if (response.IsSuccess) {
                $('#importModal').modal('hide');
            }
            Xms.Web.Alert(response.IsSuccess, response.Content);
            rebind();
        });
    }
    </script>
}